﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Yipusoft.Account;
using Yipusoft.ProfessionalTeam.DAL;

public partial class Combat_ProfessionalTeam_HelpStatOrder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDepartment();
            BindTable();
        }
    }

    void BindDepartment()
    {
        string where = " DepartmentId like '%000' and DepartmentId <>'1300000' ";
        List<Yipusoft.Account.Model.DepartmentInfo> list = Yipusoft.Account.BLL.Department.GetList(where);
        DepartmentList.DataSource = list;
        DepartmentList.DataBind();
        if (Request["DepartmentId"] != null)
        {
            DepartmentList.SelectedValue = Request["DepartmentId"];
        }
    }
    private void BindTable()
    {
        string ParentId = DepartmentList.SelectedValue;
        string where = " DepartmentId like '" + ParentId.Substring(0, 4) + "3%'";
        List<Yipusoft.Account.Model.DepartmentInfo> list = Yipusoft.Account.BLL.Department.GetList(where);
        //头
        HtmlTableRow tr = new HtmlTableRow();
        HtmlTableCell td = new HtmlTableCell();
        td.InnerText = "单位";

        tr.Cells.Add(td);
        td = new HtmlTableCell();
        td.InnerText = "专业队伍名称";
        td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
        tr.Cells.Add(td);
        td = new HtmlTableCell();
        td.InnerText = "是否达标";
        td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
        tr.Cells.Add(td);
        td = new HtmlTableCell();
        td.InnerText = "更新时间";
        td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
        tr.Cells.Add(td);
        table.Rows.Add(tr);
        TeamDataContext tdc = new TeamDataContext();
        int DepartmentRowSpan = 0;
            for (int i = 0; i < list.Count; i++)
            {
                tr = new HtmlTableRow();
                string departmentID = list[i].DepartmentID;
                string DepartmentName = list[i].FullName;
                HtmlTableCell departmentTd = new HtmlTableCell();
                departmentTd.InnerText = list[i].ShortName;
                departmentTd.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                tr.Cells.Add(departmentTd);
                DepartmentRowSpan = 0;
                HtmlTableCell procCell;
                if (Request["Type"].ToString().Equals("0"))
                {
                    List<Assault> assaults = tdc.Assault.Where(o => o.DepartmentId == departmentID).ToList();
                    for (int j = 0; j < assaults.Count; j++)
                    {   
                        procCell = new HtmlTableCell();
                        procCell.InnerText = tdc.AssaultType.Where(o => o.Id == assaults[j].TypeId).FirstOrDefault().TypeName;
                        procCell.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                        tr.Cells.Add(procCell);
                        procCell = new HtmlTableCell();
                        procCell.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                        if (Yipusoft.ProfessionalTeam.BLL.Stat.isComplianceAssaultByPid(assaults[j].Id))
                        {
                            //assaultCount++;
                            procCell.InnerHtml = "<a href=HelpNew.aspx?DepartmentID=" + departmentID + "&Aid=" + assaults[j].Id + "&Type=2>√</a>";
                        }
                        else
                        {
                            procCell.InnerHtml = "<a href=HelpNew.aspx?DepartmentID=" + departmentID + "&Aid=" + assaults[j].Id + "&Type=2>×</a>";
                        }
                        tr.Cells.Add(procCell);
                        procCell = new HtmlTableCell();
                        procCell.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                        DateTime LastUpdateDate = Yipusoft.ProfessionalTeam.BLL.Stat.GetAssaultLastUpdateDate(assaults[j].Id);
                        procCell.InnerText = LastUpdateDate == DateTime.MinValue ? "" : LastUpdateDate.ToShortDateString();
                        tr.Cells.Add(procCell);
                        DepartmentRowSpan++;
                        table.Rows.Add(tr);
                        tr = new HtmlTableRow();
                    }                   
                        departmentTd.RowSpan = DepartmentRowSpan;
                }
                if (Request["Type"].ToString().Equals("1"))
                {

                    if (!DepartmentName.Contains("特勤"))
                    {
                        //-------------抢险救援班------------------------------------------------------------                       
                        HtmlTableCell teamtd = new HtmlTableCell();
                        teamtd.InnerText = "抢险救援班";
                        teamtd.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                        tr.Cells.Add(teamtd);
                        HtmlTableCell isStandardTd = new HtmlTableCell();

                        if (Yipusoft.ProfessionalTeam.BLL.Stat.isComplianceHelpByDepartment(departmentID))
                        {
                            //HelpCount++;
                            isStandardTd.InnerHtml = "<a href='HelpNew.aspx?DepartmentID=" + departmentID + "&Type=0'>√</a>";
                        }
                        else
                        {
                            //isStandardTd.InnerHtml = "<a href='HelpNew.aspx?DepartmentID=" + departmentID + "&Type=0'>×</a>";
			    isStandardTd.InnerHtml = "<a href='HelpNew.aspx?DepartmentID=" + departmentID + "&Type=0'>√</a>";
                        }

                        isStandardTd.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                        tr.Cells.Add(isStandardTd);
                        HtmlTableCell UpdateDateTd = new HtmlTableCell();
                        UpdateDateTd.InnerHtml = Yipusoft.ProfessionalTeam.BLL.Stat.GetHelpLastUpdateTime(departmentID) == DateTime.MinValue ? "" : Yipusoft.ProfessionalTeam.BLL.Stat.GetHelpLastUpdateTime(departmentID).ToShortDateString();
                        UpdateDateTd.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                        tr.Cells.Add(UpdateDateTd);
                        table.Rows.Add(tr);
                        DepartmentRowSpan++;
                    }
                    departmentTd.RowSpan = DepartmentRowSpan;
                }
                if (Request["Type"].ToString().Equals("2"))
                {
                    List<ProfessionalProc> procs = tdc.ProfessionalProc.Where(o => o.DepartmentId == departmentID).ToList();

                    for (int j = 0; j < procs.Count; j++)
                    {
                        
                        procCell = new HtmlTableCell();
                        procCell.InnerText = tdc.ProcsType.Where(o => o.Id == procs[j].ProcessionType).FirstOrDefault().TypeName;
                        procCell.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                        tr.Cells.Add(procCell);
                        procCell = new HtmlTableCell();
                        procCell.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                        if (Yipusoft.ProfessionalTeam.BLL.Stat.isComplianceProsByPid(procs[j].id))
                        {
                           // procsCount++;
                            procCell.InnerHtml = "<a href=HelpNew.aspx?DepartmentID=" + departmentID + "&Pid=" + procs[j].id + "&Type=1>√</a>";
                        }
                        else
                        {
                            //procCell.InnerHtml = "<a href=HelpNew.aspx?DepartmentID=" + departmentID + "&Pid=" + procs[j].id + "&Type=1>×</a>";
		            procCell.InnerHtml = "<a href=HelpNew.aspx?DepartmentID=" + departmentID + "&Pid=" + procs[j].id + "&Type=1>√</a>";
                        }

                        tr.Cells.Add(procCell);
                        procCell = new HtmlTableCell();
                        procCell.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                        DateTime LastUpdateDate = Yipusoft.ProfessionalTeam.BLL.Stat.GetProsLastUpdateDateByPid(procs[j].id);
                        procCell.InnerText = LastUpdateDate == DateTime.MinValue ? "" : LastUpdateDate.ToShortDateString();
                        tr.Cells.Add(procCell);
                        DepartmentRowSpan++;
                        table.Rows.Add(tr);
                        tr = new HtmlTableRow();
                    }
                    departmentTd.RowSpan = DepartmentRowSpan;
                }



        }
        
    }

    //获取攻坚组数量
    private int GetAssaults(string departmentID)
    {
        int count = 0;
        TeamDataContext tdc = new TeamDataContext();
        List<Assault> assaults = tdc.Assault.Where(o => o.DepartmentId.Substring(0, 4) == departmentID.Substring(0, 4)).ToList();
        foreach (Assault a in assaults)
        {
            if (Yipusoft.ProfessionalTeam.BLL.Stat.isComplianceAssaultByPid(a.Id))
            {
                count += 1;
            }
        }
        return count;
    }
    protected void DepartmentList_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect("HelpStatOrder.aspx?DepartmentId=" + DepartmentList.SelectedValue+"&Type="+Request["Type"].ToString());
    }
}
